package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * setmeal分页查询
     * @param setmealDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO pageQueryDTO) {
        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(pageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO querySetmealById(Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        List<SetmealVO> setmealVOS = setmealMapper.querySetmealAsSetmealVo(setmeal);
        if (setmealVOS == null || setmealVOS.size() == 0){
            throw new BaseException(MessageConstant.EXIT_NULL_DATA);
        }
        SetmealVO setmealVO = setmealVOS.get(0);
//        获取套餐内的菜品信息
        List<SetmealDish> setmealDishs = setmealDishMapper.getSetmealDIshsBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishs);
        return setmealVO;
    }


    /**
     * 更新套餐信息
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        // 删除原有的套餐菜品信息
        setmealDishMapper.deletSetmealDishBySetmealId(setmeal.getId());

        // 把新的菜品信息插入
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.insertSetmealDishes(setmealDishes);

    }


    /**
     * 启用/禁用
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updateSetmeal(setmeal);
    }


    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        // 新增套餐--维护套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetmeal(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 维护套餐_菜品关联表
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.insertSetmealDishes(setmealDishes);
    }


    /**
     * 根据id批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteSetmealsByIds(List<Long> ids) {
        // 检查套餐是否启售，启售状态禁止删除
        List<Setmeal> setmeals = setmealMapper.querysetmealByIds(ids);
        if(setmeals != null && setmeals.size() != 0) {
            setmeals.forEach(item -> {
                if (item.getStatus() == 1){
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                }
            });
        }
        // 删除套餐
        setmealMapper.deleteSetmealsByIds(ids);
        // 删除套餐内的菜品信息
        setmealDishMapper.deletSetmealDishBySetmealIds(ids);

    }

    /**
     * 根据分类id查询启售的套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
        return setmealMapper.getSetmealByCategoryId(categoryId);
    }


    @Override
    public List<DishItemVO> getSetmealIncludedDishesById(Long id) {
        return setmealMapper.getSetmealIncludedDishesById(id);
    }
}
